<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>用户登录-成都华联</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="layui/css/layui.css" media="all">
        <!--腾讯云验证码js-->
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="layui/layui.js" charset="utf-8"></script>
        <script>var $ = layui.jquery;</script>
    </head>
    <body>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>用户登录</legend>
        </fieldset>
        <div class="layui-input-block layui-form" id="modifyClass"
             lay-filter="updateClass" style="margin: 20px;margin-top: 200px">
            <div class="layui-form-item">
                <div style="text-align: center;margin: 0 30px">
                    <input type="text" name="phone" lay-verify="required|phone" placeholder="请输入手机号"
                           autocomplete="off" id="phoneInput"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" style="text-align: center">
                <button class="layui-btn" lay-submit="" lay-filter="loginBtn">登录
                </button>
                <button class="layui-btn" id="regBtn">注册</button>
            </div>
        </div>

        <script>
            layui.use(['form', 'layedit', 'laydate'], function () {
                var form = layui.form
                    , layer = layui.layer;

                //监听登录按钮
                form.on('submit(loginBtn)', function () {
                    $.ajax({
                        url: "/user"
                        , type: "post"
                        , headers: {"method": "login"}
                        , data: {
                            "phone": $("#phoneInput").val()
                        }
                        , dataType: "json"
                        , success: function (result) {
                            if (result.code == 0) {
                                layer.msg(result.msg, {icon: 6, offset: "auto", time: 1000});
                                //添加本地localStorage
                                window.localStorage.setItem("wzphone", $("#phoneInput").val())
                                //跳转到用户主页
                                window.location.href = "/getCpnList";
                            } else if (result.code == 1) {
                                layer.confirm(result.msg + "是否去注册？", {icon: 5, offset: "auto"}
                                    , function () {
                                        //跳转到注册
                                        window.location.href = "/userReg.html";
                                        layer.close();
                                    })
                            }
                        }
                        , error: function () {
                            layer.msg("发生未知错误", {icon: 5, offset: "auto", time: 1000});
                        }
                    })
                    return false;
                });

                //监听注册按钮
                $("#regBtn").click(function () {
                    window.location.href = "/userReg.html";
                })


            });
        </script>
    </body>
</html>